#include<bits/stdc++.h>
#define int long long
#define rep(i,a,b) for(int i(a);i<=(b);++i)
#define req(i,a,b) for(int i(a);i>=(b);--i)
#define sort stable_sort
using namespace std;
char buf[1<<23],*p1=buf,*p2=buf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<23,stdin),p1==p2)?EOF:*p1++)
template<typename TP> inline TP read(TP &num)
{
	TP x=0;
	int f=0;
	char ch=getchar();
	while(ch<48||ch>57) f|=ch=='-',ch=getchar();
	while(48<=ch&&ch<=57) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
	return num=f?-x:x;
}
template<typename ...Args> inline void read(Args &...args)
{
	(read(args),...);
}
template<typename TP> inline void write(TP x)
{
	(x<0)?(putchar('-'),x=-x):0;
	(x>9)?(write(x/10),0):0;
	putchar((x%10)^48);
}
template<typename TP> inline void writeln(TP x)
{
	write<TP>(x);
	puts("");
}
signed main()
{
	freopen("trip.in","r",stdin);
	freopen("trip.out","w",stdout);
	int n; read(n);
	vector<vector<pair<int,int>>> g(n+1);
	rep(i,1,n-1)
	{
		int x=i+1,y,v; read(y,v);
		g[x].emplace_back(y,v),g[y].emplace_back(x,v);
	}
	vector<int> fa(n+1),dep=fa,siz=fa,top=fa,son=fa,dis=fa,dfn=fa; int tim=0;
	vector<pair<int,int>> leaf;
	function<void(int,int)> dfs=[&](int x,int faa)->void
	{
		siz[x]=1,dep[x]=dep[faa]+1,fa[x]=faa,dfn[x]=++tim;
		int hv=0;
		for(auto [y,len]:g[x]) if(y!=faa)
		{
			dis[y]=dis[x]+len,dfs(y,x),siz[x]+=siz[y],hv=1;
			if(siz[y]>siz[son[x]]) son[x]=y;
		}
		if(!hv) leaf.emplace_back(x,dfn[x]);
	}; dfs(1,0);
	function<void(int,int)> decomp=[&](int x,int topf)->void
	{
		top[x]=topf;
		if(son[x]) decomp(son[x],topf);
		for(auto [y,_]:g[x]) if(y!=fa[x]&&y!=son[x]) decomp(y,y);
	}; decomp(1,1);
	auto lca=[&](int x,int y)->int
	{
		while(top[x]!=top[y])
		{
			if(dep[top[x]]<dep[top[y]]) swap(x,y);
			x=fa[top[x]];
		}
		return dep[x]>dep[y]?y:x;
	};
	sort(leaf.begin(),leaf.end(),[](auto a,auto b){return a.second<b.second;});
	int ans=0;
	for(int i=0;i<(int)leaf.size();i+=2)
	{
		int x=leaf[i].first,y=leaf[i+1].first;
		ans+=dis[x]+dis[y]-2*dis[lca(x,y)];
	}
	writeln(ans);
	return 0;
}
